using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using Common;

namespace SSettings
{
    [Serializable]
    public class History
    {
        private ObservableCollection<string> _ExperimentHistory = new ObservableCollection<string>();

        private string _ExperimentSavePath = string.Empty;

        private ObservableCollection<string> _Serialnumber = new ObservableCollection<string>();

        private ObservableCollection<string> _ExperimentOperatorList = new ObservableCollection<string>();

        private ObservableCollection<string> _ExperimentAuditorList = new ObservableCollection<string>();

        private ObservableCollection<string> _ExperimentTemplateHistory = new ObservableCollection<string>();

        private ObservableCollection<string> _ProjectHistory = new ObservableCollection<string>();

        public string ExperimentSavePath
        {
            get
            {
                return _ExperimentSavePath;
            }
            set
            {
                _ExperimentSavePath = value;
            }
        }

        public ObservableCollection<string> ExperimentHistory
        {
            get
            {
                return _ExperimentHistory;
            }
            set
            {
                _ExperimentHistory = value;
            }
        }

        public ObservableCollection<string> Serialnumber
        {
            get
            {
                return _Serialnumber;
            }
            set
            {
                _Serialnumber = value;
            }
        }

        public ObservableCollection<string> ExperimentOperatorList
        {
            get
            {
                return _ExperimentOperatorList;
            }
            set
            {
                _ExperimentOperatorList = value;
            }
        }

        public ObservableCollection<string> ExperimentAuditorList
        {
            get
            {
                return _ExperimentAuditorList;
            }
            set
            {
                _ExperimentAuditorList = value;
            }
        }

        public ObservableCollection<string> ExperimentTemplateHistory
        {
            get
            {
                return _ExperimentTemplateHistory;
            }
            set
            {
                _ExperimentTemplateHistory = value;
            }
        }

        public ObservableCollection<string> ProjectHistory
        {
            get
            {
                return _ProjectHistory;
            }
            set
            {
                _ProjectHistory = value;
            }
        }

        public void AddExperimentHistory(string name)
        {
            Add(_ExperimentHistory, name);
        }

        public void RemoveExperimentHistory(string name)
        {
            Remove(_ExperimentHistory, name);
        }

        public void AddExperimentTemplateHistory(string name)
        {
            Add(_ExperimentTemplateHistory, name);
        }

        public void RemoveExperimentTemplateHistory(string name)
        {
            Remove(_ExperimentTemplateHistory, name);
        }

        public void AddProjectHistory(string name)
        {
            Add(_ProjectHistory, name);
        }

        public void RemoveProjectHistory(string name)
        {
            Remove(_ProjectHistory, name);
        }

        public void AddExperimentAuditor(string Auditor)
        {
            Add(_ExperimentAuditorList, Auditor, true);
        }

        public void AddExperimentOperator(string Operator)
        {
            Add(_ExperimentOperatorList, Operator, true);
        }

        private void Add(ObservableCollection<string> list, string name, bool isOperator = false)
        {
            if (list.Contains(name))
            {
                list.Move(list.IndexOf(name), 0);
            }
            else
            {
                list.Insert(0, name);
            }
            int num = 16;
            if (isOperator)
            {
                num = 10;
            }
            if (list.Count > num)
            {
                list.RemoveAt(num);
            }
        }

        private void Remove(ObservableCollection<string> list, string name)
        {
            if (list.Contains(name))
            {
                list.Remove(name);
            }
        }

        public void SaveToStream(StreamEx stream)
        {
            stream.Write(PropertyKey.Key_History);
            List<PropertyKey> list = new List<PropertyKey>();
            List<byte[]> list2 = new List<byte[]>();
            list.Add(PropertyKey.Key_ExperimentHistory);
            StreamEx streamEx = new StreamEx();
            streamEx.Write(BytesConverter.GetBytes(ExperimentHistory.Count));
            foreach (string item in ExperimentHistory)
            {
                streamEx.Write(item);
            }
            streamEx.Seek(0L, SeekOrigin.Begin);
            list2.Add(streamEx.ToArray());
            streamEx.Close();
            list.Add(PropertyKey.Key_ExperimentOperatorList);
            streamEx = new StreamEx();
            streamEx.Write(BytesConverter.GetBytes(_ExperimentOperatorList.Count));
            foreach (string experimentOperator in _ExperimentOperatorList)
            {
                streamEx.Write(experimentOperator);
            }
            streamEx.Seek(0L, SeekOrigin.Begin);
            list2.Add(streamEx.ToArray());
            streamEx.Close();
            list.Add(PropertyKey.Key_ExperimentAuditorList);
            streamEx = new StreamEx();
            streamEx.Write(BytesConverter.GetBytes(_ExperimentAuditorList.Count));
            foreach (string experimentAuditor in _ExperimentAuditorList)
            {
                streamEx.Write(experimentAuditor);
            }
            streamEx.Seek(0L, SeekOrigin.Begin);
            list2.Add(streamEx.ToArray());
            streamEx.Close();
            list.Add(PropertyKey.Key_Serialnumber);
            streamEx = new StreamEx();
            streamEx.Write(BytesConverter.GetBytes(_Serialnumber.Count));
            foreach (string item2 in _Serialnumber)
            {
                streamEx.Write(item2);
            }
            streamEx.Seek(0L, SeekOrigin.Begin);
            list2.Add(streamEx.ToArray());
            streamEx.Close();
            list.Add(PropertyKey.Key_ExperimentSavePath);
            list2.Add(BytesConverter.GetBytes(ExperimentSavePath));
            stream.Write(PropertyKey.Key_Index);
            stream.WriteIndex(list, list2);
            stream.Flush();
        }

        public bool ReadFromStream(StreamEx stream)
        {
            PropertyKey propertyKey = stream.ReadKey();
            if (propertyKey != PropertyKey.Key_History)
            {
                stream.Seek(-4L, SeekOrigin.Current);
                return false;
            }
            propertyKey = stream.ReadKey();
            if (propertyKey == PropertyKey.Key_Index)
            {
                List<PropertyKey> keys;
                List<byte[]> dates;
                stream.ReadIndex(out keys, out dates);
                bool flag = true;
                for (int i = 0; i < keys.Count; i++)
                {
                    if (!flag)
                    {
                        return false;
                    }
                    StreamEx streamEx = null;
                    int num = 0;
                    switch (keys[i])
                    {
                        case PropertyKey.Key_ExperimentHistory:
                            {
                                streamEx = new StreamEx();
                                streamEx.Write(dates[i]);
                                streamEx.Seek(0L, SeekOrigin.Begin);
                                num = streamEx.ReadInt();
                                for (int n = 0; n < num; n++)
                                {
                                    string item5 = streamEx.ReadString();
                                    ExperimentHistory.Add(item5);
                                }
                                streamEx.Close();
                                break;
                            }
                        case PropertyKey.Key_ExperimentTemplateHistory:
                            {
                                streamEx = new StreamEx();
                                streamEx.Write(dates[i]);
                                streamEx.Seek(0L, SeekOrigin.Begin);
                                num = streamEx.ReadInt();
                                for (int l = 0; l < num; l++)
                                {
                                    string item3 = streamEx.ReadString();
                                    ExperimentTemplateHistory.Add(item3);
                                }
                                streamEx.Close();
                                break;
                            }
                        case PropertyKey.Key_ProjectHistory:
                            {
                                streamEx = new StreamEx();
                                streamEx.Write(dates[i]);
                                streamEx.Seek(0L, SeekOrigin.Begin);
                                num = streamEx.ReadInt();
                                for (int num2 = 0; num2 < num; num2++)
                                {
                                    string item6 = streamEx.ReadString();
                                    ProjectHistory.Add(item6);
                                }
                                streamEx.Close();
                                break;
                            }
                        case PropertyKey.Key_ExperimentOperatorList:
                            {
                                streamEx = new StreamEx();
                                streamEx.Write(dates[i]);
                                streamEx.Seek(0L, SeekOrigin.Begin);
                                num = streamEx.ReadInt();
                                for (int k = 0; k < num; k++)
                                {
                                    string item2 = streamEx.ReadString();
                                    ExperimentOperatorList.Add(item2);
                                }
                                streamEx.Close();
                                break;
                            }
                        case PropertyKey.Key_ExperimentAuditorList:
                            {
                                streamEx = new StreamEx();
                                streamEx.Write(dates[i]);
                                streamEx.Seek(0L, SeekOrigin.Begin);
                                num = streamEx.ReadInt();
                                for (int m = 0; m < num; m++)
                                {
                                    string item4 = streamEx.ReadString();
                                    ExperimentAuditorList.Add(item4);
                                }
                                streamEx.Close();
                                break;
                            }
                        case PropertyKey.Key_Serialnumber:
                            {
                                streamEx = new StreamEx();
                                streamEx.Write(dates[i]);
                                streamEx.Seek(0L, SeekOrigin.Begin);
                                num = streamEx.ReadInt();
                                for (int j = 0; j < num; j++)
                                {
                                    string item = streamEx.ReadString();
                                    Serialnumber.Add(item);
                                }
                                streamEx.Close();
                                break;
                            }
                        case PropertyKey.Key_ExperimentSavePath:
                            ExperimentSavePath = BytesConverter.BytesToString(dates[i]);
                            break;
                    }
                }
                return true;
            }
            stream.Seek(-4L, SeekOrigin.Current);
            string text = null;
            int num3 = 0;
            while (true)
            {
                switch (stream.ReadKey())
                {
                    case PropertyKey.Key_ExperimentHistory:
                        {
                            num3 = stream.ReadInt();
                            for (int num5 = 0; num5 < num3; num5++)
                            {
                                text = stream.ReadString();
                                if (!string.IsNullOrEmpty(text))
                                {
                                    ExperimentHistory.Add(text);
                                }
                            }
                            break;
                        }
                    case PropertyKey.Key_ExperimentTemplateHistory:
                        {
                            num3 = stream.ReadInt();
                            for (int num6 = 0; num6 < num3; num6++)
                            {
                                text = stream.ReadString();
                                if (!string.IsNullOrEmpty(text))
                                {
                                    ExperimentTemplateHistory.Add(text);
                                }
                            }
                            break;
                        }
                    case PropertyKey.Key_ProjectHistory:
                        {
                            num3 = stream.ReadInt();
                            for (int num4 = 0; num4 < num3; num4++)
                            {
                                text = stream.ReadString();
                                if (!string.IsNullOrEmpty(text))
                                {
                                    ProjectHistory.Add(text);
                                }
                            }
                            break;
                        }
                    default:
                        stream.Seek(-4L, SeekOrigin.Current);
                        return true;
                }
            }
        }
    }
}
